<template>
  <div class="app-container">
    <h2 style="text-align: center;">特产列表</h2>
    <el-button
      type="text"
      @click="speciesFormVisible = true
              species = {name: '', yield: ''}
              speciesId = ''">添加特产</el-button>


    <el-table :data="speciesList" style="width: 100%">
      <el-table-column label="特产名称" align="center" prop="speciesName"/>
      <el-table-column label="产量 (斤/亩)" align="center" prop="yield">
        <template slot-scope="scope">
          <span v-if="scope.row.yield == undefined"> 评估中 </span>
          <span v-else> {{ scope.row.yield }} </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="editSpecies(scope.row.id)">编辑</el-button>
          <el-button type="text" @click="deleteSpecies(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="speciesFormVisible" title="编辑一级分类">
      <el-form :model="species" label-width="120px">
        <el-form-item label="特产名称">
          <el-input v-model="species.speciesName"/>
        </el-form-item>
        <el-form-item label="今年产量">
          <el-input v-model="species.yield"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="speciesFormVisible = false">取 消</el-button>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>

  </div>

</template>
<script>
import speciesApi from '@/api/trace/species'

export default {
  data() {
    return {
      speciesId: '',
      speciesList: [],
      saveBtnDisabled: false,
      speciesFormVisible: false,
      species: { // 一级分类对象
        name: '',
        yield: ''
      }
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.getList()
    },
    getList() {
      speciesApi.getList().then(response => {
        this.speciesList = response.data.list
      })
    },

    saveOrUpdate() {
      this.saveBtnDisabled = true
      if (this.speciesId === undefined || this.speciesId === '') {
        this.add()
      } else {
        this.update()
      }
    },
    add() {
      speciesApi.add(this.species).then(response => {
        this.$message({
          type: 'success',
          message: '保存成功!'
        })
        this.helpSave()
      }).catch((response) => {
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },
    update() {
      this.species.speciesId = this.speciesId
      speciesApi.update(this.species).then(response => {
        this.$message({
          type: 'success',
          message: '修改成功!'
        })
        this.helpSave()
      }).catch((response) => {
        // console.log(response)
        this.$message({
          type: 'error',
          message: response.message
        })
      })
    },

    helpSave() {
      this.speciesFormVisible = false// 如果保存成功则关闭对话框
      this.getList()// 刷新列表
      this.species = {}
      this.saveBtnDisabled = false
    },
    editSpecies(id) {
      this.speciesId = id
      this.speciesFormVisible = true
      speciesApi.get({ id: id }).then(response => { this.species = response.data.item })
    },
    deleteSpecies(id) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return speciesApi.delete({ id: id })
      }).then(() => {
        this.getList()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch((response) => { // 失败
        if (response === 'cancel') {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        } else {
          this.$message({
            type: 'error',
            message: response.data.message
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.levelOneList{
  position: relative;
  list-style: none;
  margin: 0;
  padding: 0;
}
.levelOneList li{
  position: relative;
}
.levelOneList p{
  float: left;
  font-size: 20px;
  margin: 10px 0;
  padding: 10px;
  height: 70px;
  line-height: 50px;
  width: 100%;
  border: 1px solid #DDD;
}

.levelOneList .acts {
  float: right;
  font-size: 14px;
}
.videoList{
  padding-left: 50px;
}
.videoList p{
  float: left;
  font-size: 14px;
  margin: 10px 0;
  padding: 10px;
  height: 50px;
  line-height: 30px;
  width: 100%;
  border: 1px dotted #DDD;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 300px;
  height: 200px;
  line-height: 200px;
  text-align: center;
}
.avatar {
  width: 300px;
  height: 200px;
  display: block;
}
</style>
